<template>
  <div class="postList">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="">文章管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card" style="margin-top: 20px">
      <!-- 表格 -->
      <el-table :data="postList" style="width: 100%" border>
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="title" label="标题" width="280">
        </el-table-column>
        <el-table-column label="类型" width="180">
          <template slot-scope="scope">
            {{ scope.row.type == 1 ? "文章" : "视频" }}
          </template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="40"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getPostList } from "@/apis/post.js";
export default {
  data() {
    return {
      postList: [],
      // 每页显示的数量
      pageSize: 5,
      // 当前页码
      pageIndex: 4,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 获取文章列表的分页数据
    async init() {
      let res = await getPostList({
        keyword: "",
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      });
      console.log(res);
      this.postList = res.data.data;
    },
    handleEdit(data) {
      console.log(data);
    },
    // 切换数量下拉列表时触发事件
    // 参数就是当前用户所选择的数量
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      // 1、重置变量pageSize
      this.pageSize = val;
      // 2、根据这个变量发起数据请求，渲染页面
      this.init();
    },
    //单击页面时触发
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      // 1、重置变量pageIndex
      this.pageIndex = val;
      // 2、根据这个变量发起数据请求，渲染数据
      this.init();
    },
  },
};
</script>

<style>
</style>